<?php
declare(strict_types=1);


namespace App\Fastapp\Mapper;

use App\Fastapp\Model\Currency;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * 币种列表Mapper类
 */
class FastappCurrencyMapper extends AbstractMapper
{
    /**
     * @var Currency
     */
    public $model;

    public function assignModel()
    {
        $this->model = Currency::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        // 币种代码
        if (isset($params['symbol']) && filled($params['symbol'])) {
            $query->where('symbol', '=', $params['symbol']);
        }

        // 0主链 >0子链
        if (isset($params['parent_id']) && filled($params['parent_id'])) {
            $query->where('parent_id', '=', $params['parent_id']);
        }

        // 显示
        if (isset($params['is_show']) && filled($params['is_show'])) {
            $query->where('is_show', '=', $params['is_show']);
        }

        // 1法币2加密币3加密法币
        if (isset($params['type']) && filled($params['type'])) {
            $query->where('type', '=', $params['type']);
        }

        // 充值0否1是
        if (isset($params['is_deposit']) && filled($params['is_deposit'])) {
            $query->where('is_deposit', '=', $params['is_deposit']);
        }

        // 0下架1正常
        if (isset($params['status']) && filled($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        return $query;
    }

    public function save(array $data): mixed
    {
        $s = parent::save($data);
        set_currency_cache();
        return $s;
    }

    public function update(mixed $id, array $data): bool
    {
        $s = parent::update($id, $data);
        set_currency_cache();
        return $s;
    }
}